﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ProgNetComponents.Graphic
{
    public static class GraphicTools
    {
        public static Color HSL_to_RGB(double Hue, double Saturation, double Lightness)
        {
            double r=0,g=0,b=0;
            double temp1,temp2;

            if (Lightness == 0)
            {
                r = g = b = 0;
            }
            else
            {
                if (Saturation == 0)
                {
                    r = g = b = Lightness;
                }
                else
                {
                    temp2 = ((Lightness <= 0.5) ? Lightness * (1.0 + Saturation) : Lightness + Saturation - (Lightness * Saturation));
                    temp1 = 2.0 * Lightness - temp2;

                    double[] t3=new double[] { Hue + 1.0 / 3.0, Hue, Hue - 1.0 / 3.0 };
                    double[] clr=new double[] { 0, 0, 0 };

                    for (int i=0; i < 3; i++)
                    {
                        if (t3[i] < 0)
                            t3[i] += 1.0;

                        if (t3[i] > 1)
                            t3[i] -= 1.0;

                        if (6.0 * t3[i] < 1.0)
                            clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
                        else if (2.0 * t3[i] < 1.0)
                            clr[i] = temp2;
                        else if (3.0 * t3[i] < 2.0)
                            clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
                        else
                            clr[i] = temp1;
                    }

                    r = clr[0];
                    g = clr[1];
                    b = clr[2];
                }
            }

            return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
        }
    }
}
